package permission

import (
	"gorm.io/gorm"
)

// PermissionMigrator 权限模块迁移器
type PermissionMigrator struct{}

// NewPermissionMigrator 创建权限模块迁移器
func NewPermissionMigrator() *PermissionMigrator {
	return &PermissionMigrator{}
}

// GetName 返回模块名称
func (m *PermissionMigrator) GetName() string {
	return "permission"
}

// GetModels 返回需要迁移的模型
func (m *PermissionMigrator) GetModels() []interface{} {
	return []interface{}{&Permission{}}
}

// GetDependencies 返回依赖的模块名称
func (m *PermissionMigrator) GetDependencies() []string {
	return []string{} // 权限模块不依赖其他模块
}

// Migrate 执行模块特定的迁移逻辑
func (m *PermissionMigrator) Migrate(db *gorm.DB) error {
	// 权限模块不需要额外的迁移逻辑
	return nil
}

// Seed 执行模块数据初始化
func (m *PermissionMigrator) Seed(db *gorm.DB) error {
	// 检查是否已有权限数据
	var count int64
	db.Model(&Permission{}).Count(&count)
	if count > 0 {
		return nil // 已有数据，跳过初始化
	}

	// 初始化权限数据
	permissions := []Permission{
		// 仪表板
		{
			ID:          1,
			Name:        "仪表板",
			Code:        "dashboard",
			DisplayName: "仪表板",
			Type:        "menu",
			ParentID:    uintPtr(0),
			Path:        "/dashboard",
			Icon:        "HomeFilled",
			Sort:        1,
			Status:      1,
			Description: "系统仪表板",
		},
		// 系统管理
		{
			ID:          2,
			Name:        "系统管理",
			Code:        "system",
			DisplayName: "系统管理",
			Type:        "menu",
			ParentID:    uintPtr(0),
			Path:        "/system",
			Icon:        "Setting",
			Sort:        2,
			Status:      1,
			Description: "系统管理模块",
		},
		// 用户管理
		{
			ID:          3,
			Name:        "用户管理",
			Code:        "system:user",
			DisplayName: "用户管理",
			Type:        "menu",
			ParentID:    uintPtr(2),
			Path:        "/users",
			Icon:        "User",
			Sort:        1,
			Status:      1,
			Description: "用户管理功能",
		},
		{
			ID:          4,
			Name:        "查看用户",
			Code:        "system:user:read",
			DisplayName: "查看用户",
			Type:        "button",
			ParentID:    uintPtr(3),
			Sort:        1,
			Status:      1,
			Description: "查看用户列表和详情",
		},
		{
			ID:          5,
			Name:        "创建用户",
			Code:        "system:user:create",
			DisplayName: "创建用户",
			Type:        "button",
			ParentID:    uintPtr(3),
			Sort:        2,
			Status:      1,
			Description: "创建新用户",
		},
		{
			ID:          6,
			Name:        "编辑用户",
			Code:        "system:user:update",
			DisplayName: "编辑用户",
			Type:        "button",
			ParentID:    uintPtr(3),
			Sort:        3,
			Status:      1,
			Description: "编辑用户信息",
		},
		{
			ID:          7,
			Name:        "删除用户",
			Code:        "system:user:delete",
			DisplayName: "删除用户",
			Type:        "button",
			ParentID:    uintPtr(3),
			Sort:        4,
			Status:      1,
			Description: "删除用户",
		},
		// 角色管理
		{
			ID:          8,
			Name:        "角色管理",
			Code:        "system:role",
			DisplayName: "角色管理",
			Type:        "menu",
			ParentID:    uintPtr(2),
			Path:        "/roles",
			Icon:        "UserFilled",
			Sort:        2,
			Status:      1,
			Description: "角色管理功能",
		},
		{
			ID:          9,
			Name:        "查看角色",
			Code:        "system:role:read",
			DisplayName: "查看角色",
			Type:        "button",
			ParentID:    uintPtr(8),
			Sort:        1,
			Status:      1,
			Description: "查看角色列表和详情",
		},
		{
			ID:          10,
			Name:        "创建角色",
			Code:        "system:role:create",
			DisplayName: "创建角色",
			Type:        "button",
			ParentID:    uintPtr(8),
			Sort:        2,
			Status:      1,
			Description: "创建新角色",
		},
		{
			ID:          11,
			Name:        "编辑角色",
			Code:        "system:role:update",
			DisplayName: "编辑角色",
			Type:        "button",
			ParentID:    uintPtr(8),
			Sort:        3,
			Status:      1,
			Description: "编辑角色信息",
		},
		{
			ID:          12,
			Name:        "删除角色",
			Code:        "system:role:delete",
			DisplayName: "删除角色",
			Type:        "button",
			ParentID:    uintPtr(8),
			Sort:        4,
			Status:      1,
			Description: "删除角色",
		},
		// 权限管理
		{
			ID:          13,
			Name:        "权限管理",
			Code:        "system:permission",
			DisplayName: "权限管理",
			Type:        "menu",
			ParentID:    uintPtr(2),
			Path:        "/permissions",
			Icon:        "Lock",
			Sort:        3,
			Status:      1,
			Description: "权限管理功能",
		},
		{
			ID:          14,
			Name:        "查看权限",
			Code:        "system:permission:read",
			DisplayName: "查看权限",
			Type:        "button",
			ParentID:    uintPtr(13),
			Sort:        1,
			Status:      1,
			Description: "查看权限列表和详情",
		},
		{
			ID:          15,
			Name:        "创建权限",
			Code:        "system:permission:create",
			DisplayName: "创建权限",
			Type:        "button",
			ParentID:    uintPtr(13),
			Sort:        2,
			Status:      1,
			Description: "创建新权限",
		},
		{
			ID:          16,
			Name:        "编辑权限",
			Code:        "system:permission:update",
			DisplayName: "编辑权限",
			Type:        "button",
			ParentID:    uintPtr(13),
			Sort:        3,
			Status:      1,
			Description: "编辑权限信息",
		},
		{
			ID:          17,
			Name:        "删除权限",
			Code:        "system:permission:delete",
			DisplayName: "删除权限",
			Type:        "button",
			ParentID:    uintPtr(13),
			Sort:        4,
			Status:      1,
			Description: "删除权限",
		},
	}

	// 创建权限
	for _, perm := range permissions {
		if err := db.Create(&perm).Error; err != nil {
			return err
		}
	}

	return nil
}

// uintPtr 返回uint指针
func uintPtr(u uint) *uint {
	return &u
}
